<?php
/**
 * ==============================================
 * Copy right 2015-2016 http://wei.meididi88.com
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @author: hy
 * @date: 2015-9-10
 * @version: v1.0.0
 */
namespace Common\Model;

use Think\Model;

/**
 * 美容师工作经历Model模型类
 */
class WorkExperienceModel extends Model{
	
	/**
	 * 生成查询条件
	 * 
	 * @param array $where
	 * @return array
	 */
	public function createWhere($where) {
		if ($this->type) {
			$where ['type'] = $this->type;
		}
		
		return $where;
	}
	
	/**
	 * 清除实例对象图片缓存
	 *
	 * @param number $target_id	美容师编号
	 */
	public function clearCache($target_id) {
		$key = $this->getCacheKey($target_id);
		S($key, null);
	}
	
	/**
	 * 编辑实例的图片
	 *
	 * @param number $target_id		美容师编号
	 * @param array $experiences	工作经历
	 * @return boolean
	 */
	public function editTargetExperiences($target_id, $submitExperiences) {
		$expeiriences = $this->getTargetExperiences($target_id);
		$beautyParlors = array();
		if ($expeiriences) {
			$beautyParlors = array_map(function ($var) {
				return $var ['beauty_parlor_name'];
			}, $expeiriences);
		}
		
		$submitBeautyParlorName = array_map(function ($var) {
			return $var['beauty_parlor_name'];
		}, $submitExperiences);
	
		$deleteExperiences = array_diff($beautyParlors, $submitBeautyParlorName);
		$addExperiences = array_diff($submitBeautyParlorName, $beautyParlors);
	
		if ($deleteExperiences) {
			foreach ($deleteExperiences as $dKey => $dValue) {
				$index = array_search($dValue, $beautyParlors);
				if ($index !== false) {
					$deleteResult = $this->where(array('id' => $expeiriences [$index] ['id']))->delete();
					if (!$deleteResult) {
						return false;
					}
				}
			}
			//触发文件清除方法
		}
		
		if ($addExperiences) {
			foreach ($addExperiences as $aKey => $aValue) {
				$index = array_search($aValue, $submitBeautyParlorName);
				if ($index !== false) {
					$experienceData = $submitExperiences [$index];
					$experienceData ['target_id'] = $target_id;
					$experienceData ['created'] = time();
					
					$addResult = $this->add($experienceData);
					if (!$addResult) {
						return false;
					}
				}
			}
		}
		
		//更新修改内容
		$same = array_intersect($beautyParlors, $submitBeautyParlorName);
		if ($same) {
			foreach ($same as $sKey => $sValue) {
				$oIndex = array_search($sValue, $beautyParlors);
				$sIndex = array_search($sValue, $submitBeautyParlorName);
				
				unset($expeiriences [$oIndex] ['status']);
				unset($expeiriences [$oIndex] ['created']);
				if (($hasModify = array_diff($expeiriences [$oIndex], $submitExperiences [$sIndex]))) {
					$this->where(array('id' => $sValue))->save($hasModify);
				}
			}
		}
	
		$this->clearCache($target_id);
		return true;
	}
	
	/**
	 * 获取某个实例图片最大排序
	 * 
	 * @param number $target_id	美容师编号
	 * @return Ambigous <number, unknown>
	 */
	public function getSortByTarget($target_id) {
		$where = $this->createWhere(array(
			'target_id'	=> $target_id
		));
		
		$result = $this->field(array('sort'))
					->where($where)
					->order('sort DESC')
					->limit(1)
					->find();
		return $result?$result['sort']:1;
	}
	
	/**
	 * 生成缓存key
	 * 
	 * @param number $target_id	美容师编号
	 * @return string
	 */
	public function getCacheKey($target_id) {
		return 'Experience_'.$target_id;
	}
	
	/**
	 * 获取实例所有图片
	 * 
	 * @param number $target_id	美容师编号
	 * @return boolean|array
	 */
	public function getTargetExperiences($target_id) {
		$key = $this->getCacheKey($target_id);
		
		$photos = S($key);
		if (!$photos) {
			$where = $this->createWhere(array(
				'target_id'	=> $target_id
			));
			$photos = $this->where($where)->select();
			S($key, $photos);
		}
		
		return $photos;
	}
	
}